package com.bytecollege.service.Impl;

    /**
     * 用户登录
     * @param username 用户名
     * @param userPwd 用户密码
     * @return
     */

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytecollege.mapper.UserMapper;
import com.bytecollege.model.User;
import com.bytecollege.service.ex.*;

import com.bytecollege.vo.AdminUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: UserServiceImpl
 * @Description:
 * @Author: Sun
 * @Date: 2022/10/18 21:03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements com.bytecollege.service.UserService {
       @Autowired
      private UserMapper userMapper;

    @Value("${spring.mail.username}")
    private String from;   // 邮件发送人

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 用户注册
     * @param user 对象
     * @return row
     */

    public int reg(User user) {
        String userEmail = user.getUserEmail();
        LambdaQueryWrapper<User> eq = new QueryWrapper<User>().lambda().eq(User::getUserEmail, userEmail);
        User result = userMapper.selectOne(eq);
        if (result!=null){
            throw new ReUserNameException("用户名被占用！");
        }
        String userPwd = user.getUserPwd();
        String s = SecureUtil.md5(userPwd);
        user.setUserPwd(s);
//        设置为普通会员
        user.setUserIsdelete(0);
        user.setUserRole(0);
        int row = userMapper.insert(user);
        if (row!=1){
            throw new InsertException("添加用户失败");
        }
        return row;
    }

    /**
     * 根据UserId 查找用户
     * @param userId
     * @return
     */
    public User getById(Integer userId){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>().eq("User_Id",userId);
        return  userMapper.selectOne(userQueryWrapper);
    }

    /**
     * 用户登录
     * @param userEmail
     * @return
     */

    public User login(String userEmail) {
        LambdaQueryWrapper<User> eq = new QueryWrapper<User>().lambda().eq(User::getUserEmail, userEmail);
        return  userMapper.selectOne(eq);
    }

    public boolean updatePassword(Integer userId,
                               String oldPassword,
                               String newPassword) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>().eq("User_Id",userId);
        User result = userMapper.selectOne(userQueryWrapper);
//        将数据库的密码进行获取出
        String password =result.getUserPwd();
        String s = SecureUtil.md5(newPassword);
        if (!password.equals(SecureUtil.md5(oldPassword))){
           return false;
        }
        UpdateWrapper<User> ut = new UpdateWrapper<User>().set("User_Pwd", s).eq("User_Id", userId);
        int update = userMapper.update(result, ut);
        if (update!=1){
            throw new updateException("修改时，出现了未知的错误！");
        }
        return true;
    }




    public void updateInfo(User user){
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getUserId,user.getUserId())
                .set(User::getUserName,user.getUserName())
                .set(User::getUserPhone,user.getUserPhone())
                .set(User::getUserBirthday,user.getUserBirthday())
                .set(User::getUserGender,user.getUserGender())
                .set(User::getUserHeadphoto,user.getUserHeadphoto())
                .set(User::getUserNickname,user.getUserNickname())
                .set(User::getUserEmail,user.getUserEmail());
        userMapper.update(null,wrapper);
    }

    /**
     * 消息发送
     * @param to
     * @param subject
     * @param context
     */

    @Override
    public void sendMsg(String to, String subject, String context) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        mailMessage.setFrom(from);
        mailMessage.setTo(to);
        mailMessage.setSubject(subject);
        mailMessage.setText(context);

        // 真正的发送邮件操作，从 from到 to
        mailSender.send(mailMessage);
    }
}